package org.tlipoca.smarticle.listener;


import com.rabbitmq.client.Channel;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.genntii.smdomain.entity.Article;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;
import org.tlipoca.smarticle.service.EsArticleService;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Component
@RequiredArgsConstructor
@Slf4j
public class ArticleEsListener {

    @Resource
    private EsArticleService esArticleService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "article.queue",durable = "true"),
            exchange = @Exchange(name = "article.topic",type = "topic"),
            key = "article.routingKey"
    ))
    public void receiver(Map map, Channel channel, Message message) throws IOException {
        // type: INSERT DELETE UPDATE SELECT
        String type = String.valueOf(((HashMap.Entry)(map).entrySet().toArray()[12]).getValue());

        log.info("监听到数据修改，类型为："+type);

        switch (type){
            case "INSERT":
                esArticleService.insertArticle(getArticleByObjectArrayMap(map));
                break;
            case "DELETE":
                esArticleService.deleteArticle(getArticleByObjectArrayMap(map));
                break;
            case "UPDATE":
                esArticleService.deleteArticle(getArticleByObjectArrayMap(map));
                esArticleService.insertArticle(getArticleByObjectArrayMap(map));
                break;
            case "SELECT", "CREATE", "ERASE":
            default:
                break;
        }

        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    public static Article getArticleByObjectArrayMap(Map map){
        Object[] array = ((HashMap) ((ArrayList) ((HashMap.Entry) (map).entrySet().toArray()[0]).getValue()).get(0)).entrySet().toArray();
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        Article article = Article.builder()
                .id(Long.valueOf(String.valueOf(((Map.Entry)array[0]).getValue())))
                .userId(Long.valueOf(String.valueOf(((Map.Entry)array[1]).getValue())))
                .channelId(Long.valueOf(String.valueOf(((Map.Entry)array[2]).getValue())))
                .title(String.valueOf(((Map.Entry)array[3]).getValue()))
                .like(Integer.valueOf(String.valueOf(((Map.Entry)array[4]).getValue())))
                .dislike(Integer.valueOf(String.valueOf(((Map.Entry)array[5]).getValue())))
//                .cityCode(Integer.valueOf(String.valueOf(((Map.Entry)array[6]).getValue())))
                .city(String.valueOf(((Map.Entry)array[7]).getValue()))
                .status(Byte.parseByte(String.valueOf(((Map.Entry)array[8]).getValue())))
                .createTime(LocalDateTime.parse(String.valueOf(((Map.Entry)array[9]).getValue()),fmt))
                .updateTime(LocalDateTime.parse(String.valueOf(((Map.Entry)array[9]).getValue()),fmt))
                .view(Integer.valueOf(String.valueOf(((Map.Entry)array[11]).getValue())))
//                .userFollowerCount(Integer.valueOf(String.valueOf(((Map.Entry)array[12]).getValue())))
                .build();
        return article;
    }

}
